package yambal.net.google.Spreadsheet
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	public class WorkSheet
	{
		public var sheet:Sheet;
		
		public var id:String;
		public var updated:Date;
		public var title:String
		
		public var rowCount:int;
		public var colCount:int;
		
		private var _list_url:String;
		
		public var Hash:Array;
		
		public function set list_url(url:String):void{
			this._list_url = url;
			this.setList();
		}
		
		public function WorkSheet()
		{
		}
		
		private var _is_busy:Boolean = false;
		private function isBusy(f:Boolean):void{
			if(f != this._is_busy){
				this._is_busy = f;
				this.sheet.childIsBusy(this._is_busy, this.id);
			}
		}
		
		public function toString():String{
			var ret:String = "id:" + this.id + ", " + 
					"updated:" + this.updated + ", " + 
					"title:" + this.title + ", " + 
					"row:" + this.rowCount + ", " + 
					"col:" + this.colCount + ", " + 
					"list_url:" + this._list_url + ", ";
			return ret;
		}

		public function setList():void{
			this.isBusy(true);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.onResponseGetList);
			loader.addEventListener(IOErrorEvent.IO_ERROR, this.onIOEorGetList);
			//loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.onStatusGetList);
			
			var req:URLRequest = new URLRequest();
			req.url = this._list_url;
			req.method = URLRequestMethod.GET;
			
			req.requestHeaders.push(new URLRequestHeader('Authorization', 'GoogleLogin auth=' + this.sheet.spreadsheet.Auth.auth));
			req.requestHeaders.push(new URLRequestHeader('Content-Type','application/atom+xml'));			//req.requestHeaders.push(new URLRequestHeader('GData-Version', '2'));
			loader.load(req);
		}
		
		private function onIOEorGetList(e:IOErrorEvent):void{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.onResponseGetList);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOEorGetList);
			//loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.onStatusGetList);
			trace("IOE:" + this.title);
			
			this.isBusy(false);
		}
		
		private function onResponseGetList(e:Event):void{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.onResponseGetList);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOEorGetList);
			//loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.onStatusGetList);
			
			var res:XML = new XML(loader.data);
			
			var atom:Namespace = res.namespace();
			var openSearch:Namespace = res.namespace('openSearch');
			var gsx:Namespace = res.namespace('gsx');

			this.Hash = new Array();
			for each (var entry:XML in res.atom::entry) {
				var cols:Array = this.parseToRowArray(entry);
				this.Hash.push(cols);
			}
			
			this.isBusy(false);
		}
		
		private function parseToRowArray(entry:XML):Array{
			trace(entry);
			
			var ret:Array = new Array();
			
			var ts:RegExp = new RegExp("<[^>]*>", "gim");
			
			var gsxTest:RegExp = new RegExp("<gsx:[^>]*>[^<]*<\/gsx:[^>]*>","gim");
			var match:Array = entry.toString().match(gsxTest);
			for each (var col:String in match) {
				col = col.replace(ts,"");
				ret.push(col);
			}
			
			return ret;
		}
	}
}